package day06;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 改正下面程序的错误,使得程序可以正常运行
 * 并能够正常的给服务端发送消息
 *
 */
public class Client {
	private Socket socket;

	public Client() {
		try {
			System.out.println("正在链接服务端...");
			//不用重新定义Socket   localhost写错
			socket = new Socket("localhost",8089);
			System.out.println("与服务端建立链接!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void start() {
		try {

			OutputStream out = socket.getOutputStream();
			//new OutputStreamWriter 和StandardCharsets字符集写错了
			OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
			BufferedWriter bw = new BufferedWriter(osw);
			//参数加上自动行刷新true
			PrintWriter pw = new PrintWriter(bw,true);

			pw.println("你好服务端!");
//
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				socket.close();//关闭socket
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		Client client = new Client();
		client.start();
	}
}









